package com.ahdy.mapper;

import com.ahdy.entity.Medication;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

// 5. 药物信息Mapper
@Mapper
public interface MedicationMapper {

    @Insert("INSERT INTO medications(name, type, manufacturer, description, side_effects, created_at) " +
            "VALUES(#{name}, #{type}, #{manufacturer}, #{description}, #{sideEffects}, #{createdAt})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Medication medication);

    @Update("UPDATE medications SET name=#{name}, type=#{type}, manufacturer=#{manufacturer}, " +
            "description=#{description}, side_effects=#{sideEffects} WHERE id=#{id}")
    int update(Medication medication);

    @Delete("DELETE FROM medications WHERE id=#{id}")
    int deleteById(Integer id);

    @Select("SELECT * FROM medications WHERE id=#{id}")
    Medication findById(Integer id);

    @Select("SELECT * FROM medications ORDER BY name ASC")
    List<Medication> findAll();

    @Select("SELECT * FROM medications WHERE name LIKE CONCAT('%', #{name}, '%') ORDER BY name ASC")
    List<Medication> findByNameLike(String name);

    @Select("SELECT * FROM medications WHERE type=#{type} ORDER BY name ASC")
    List<Medication> findByType(Medication.MedicationType type);

    @Select("SELECT * FROM medications WHERE manufacturer LIKE CONCAT('%', #{manufacturer}, '%') ORDER BY name ASC")
    List<Medication> findByManufacturerLike(String manufacturer);

    // 获取常用药物统计
    List<Map<String, Object>> getMedicationUsageStatistics();
}
